package org.delphy.indy2.containers
{
	import flash.display.GradientType;
	import flash.display.Graphics;
	import flash.geom.Matrix;
	
	import mx.containers.TabNavigator;
	import mx.events.FlexEvent;
	import mx.styles.StyleManager;
	import mx.utils.GraphicsUtil;
	
	import org.delphy.indy2.containers.carbonTabNavigatorClasses.*;
	import org.delphy.indy2.core.ICarbonComponent;

	//--------------------------------------
	//  Styles
	//--------------------------------------

	/**
	 * Tabbar style name
	 * 
	 * @default "carbonTabNavigatorTabBar"
	 */
	[Style(name="tabBarStyleName", type="String", inherit="no")]
	
	/**
	 * Corner radius
	 * 
	 * @default 6
	 */
	[Style(name="cornerRadius", type="Number", inherit="no")]
	
	/**
	 * FilterLayer 
	 * 
	 * @default: carbonTabNavigatorShadowFilter
	 */
	[Style(name="filterLayer", type="String", inherit="no")]
	
	/**
	 * Backbround Colors
	 * 
	 * @default #333333, #333333
	 */
	[Style(name="backgroundColors", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround Alphas
	 * 
	 */
	[Style(name="backgroundAlphas", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround Ratios
	 */
	[Style(name="backgroundRatios", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Backbround RatioUnits
	 */
	[Style(name="backgroundRatioUnits", type="Array", arrayType="String", inherit="no")]
		
	/**
	 * Border Colors
	 * 
	 * @default #333333, #666666
	 */
	[Style(name="borderColors", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Border Alphas
	 */
	[Style(name="borderAlphas", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Border Ratios
	 */
	[Style(name="borderRatios", type="Array", arrayType="Number", inherit="no")]
	
	/**
	 * Border RatioUnits
	 */
	[Style(name="borderRatioUnits", type="Array", arrayType="String", inherit="no")]
	
	/**
	 * Border Weight
	 * 
	 * @default 1
	 */
	[Style(name="borderWeight", type="Number", inherit="no")]
	
	public class CarbonTabNavigator extends TabNavigator implements ICarbonComponent
	{
		/** Default Skin
		 * @default org.delphy.indy2.containers.carbonTabNavigatorClasses.CarbonTabNavigator
		 */
		protected var defaultSkin:Class = CarbonTabNavigatorSkin;
		
		/**
		 *	Constructor
		 */						
		public function CarbonTabNavigator()
		{
			super();

			addEventListener(FlexEvent.CREATION_COMPLETE,onInit);
		}
		
		/**
		 * @protected
		 * Handler for FlexEvent.CREATION_COMPLETE
		 */
		protected function onInit(evt:FlexEvent):void{
			// Initialize tabbar control
			tabBar.styleName=getStyle("tabBarStyleName");
			if(!tabBar.styleName)
				tabBar.styleName=StyleManager.getStyleDeclaration(".carbonTabNavigator").getStyle("tabBarStyleName");
			
			tabBar.width=width;
		}
		
		/**
		 * @protected
		 */
		override protected function commitProperties() : void{
			super.commitProperties();
			
			// Fallback to default skin
			if(!styleName)
				styleName="carbonTabNavigator";
			
			if(!getStyle("borderSkin"))
				setStyle("borderSkin", defaultSkin);				
		}
		
		/**
		 * @protected
		 */
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void{
			super.updateDisplayList(unscaledWidth,unscaledHeight);
			
			// Pass styles to skin
			var cornerRadius:Number=getStyle("cornerRadius");
			if(!cornerRadius){
				cornerRadius=StyleManager.getStyleDeclaration(".carbonTabNavigator").getStyle("cornerRadius");
				setStyle("cornerRadius", cornerRadius);
			}
			
			var borderWeight:Number=getStyle("borderWeight");
			if(!borderWeight){
				borderWeight=StyleManager.getStyleDeclaration(".carbonTabNavigator").getStyle("borderWeight");
				setStyle("borderWeight", borderWeight);
			}
			
			// Get the tabbar style name
			var tabBarStyleName:String=getStyle("tabBarStyleName");
			if(!tabBarStyleName)
				tabBarStyleName=StyleManager.getStyleDeclaration(".carbonTabNavigator").getStyle("tabBarStyleName");
			
			tabBarStyleName="."+tabBarStyleName;
			
			// Get tabbar styles
			var tabRadius:Number=StyleManager.getStyleDeclaration(tabBarStyleName).getStyle("cornerRadius");
			if(!tabRadius)
				tabRadius=StyleManager.getStyleDeclaration(".carbonTabNavigatorTabBar").getStyle("cornerRadius");
			
			var tabBarFillColors:Array=StyleManager.getStyleDeclaration(tabBarStyleName).getStyle("backgroundColors");
			if(!tabBarFillColors)
				tabBarFillColors=StyleManager.getStyleDeclaration(".carbonTabNavigatorTabBar").getStyle("backgroundColors");
			
			var aWidth:Number=tabBar.width;
			var aHeight:Number=tabBar.height;
			
			// Manually draw the tabbar
			var g:Graphics=tabBar.graphics;
			var mat:Matrix=new Matrix();
			mat.createGradientBox(aWidth,aHeight,Math.PI/2);
			
			g.clear();
			g.beginGradientFill(GradientType.LINEAR,[tabBarFillColors[0],tabBarFillColors[1]],[1,1],[0,255],mat);
			GraphicsUtil.drawRoundRectComplex(g,0,0,aWidth,aHeight,tabRadius,tabRadius,0,0);
			g.endFill();
			
			// Highlight
			mat.createGradientBox(aWidth-4,aHeight/2,Math.PI/2);
			g.beginGradientFill(GradientType.LINEAR,[0xFFFFFF,0xFFFFFF],[0.5,0.1],[0,255],mat);
			g.drawRoundRect(2,0,aWidth-4,aHeight/2,tabRadius,tabRadius);
			g.endFill();		
		}

		//--------------------------------------
		//  Implementation of ICarbonComponent
		//--------------------------------------		
		
		/**
		 * @public
		 */
		public function get defaultStyleName():String{
			return "carbonTabNavigator";
		}
	}
}